package lesson06;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class TestJdbc {
    public static void main(String[] args) throws SQLException {

        // 1. 加载驱动

        // 2. 创建链接
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mytest?characterEncoding=utf8", "root", "root");
        // 开启事务，自动提交关闭
        conn.setAutoCommit(false);

        String uname = "张三";
        String password = "123456";
        String nickname = "我下哦我下哦我下哦";

        // 3. 创建执行器
        String sql = " INSERT INTO sns_user(uname, PASSWORD, nickname) VALUES(?, ?, ?) ";
        PreparedStatement ps = conn.prepareStatement(sql);

        int index = 0;
        ps.setString(++index, uname);
        ps.setString(++index, password);
        ps.setString(++index, nickname);
        // 4. 执行sql,处理结果
        int reuslt = ps.executeUpdate();
        // 判断变更条数
        if (reuslt == 1) {
            conn.commit();
        } else {
            conn.rollback();
        }
        // 5. 关闭资源
        conn.close();

    }

}
